package com.issoft.studentresitor.controller.admin;


import com.issoft.studentresitor.domain.dto.FeeDTO;
import com.issoft.studentresitor.domain.pojo.Fee;
import com.issoft.studentresitor.domain.pojo.Result;
import com.issoft.studentresitor.domain.query.FeeQuery;
import com.issoft.studentresitor.domain.vo.FeeVo;
import com.issoft.studentresitor.service.IFeeService;
import com.issoft.studentresitor.utils.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author GJH
 * @since 2025-09-14
 */
@Slf4j
@RestController
@RequestMapping("/admin/fee")
@Api(tags = "缴费管理")
public class FeeController {
    @Autowired
    private IFeeService feeService;

    @GetMapping("/page")
    @ApiOperation("分页查询缴费记录")
    public Result queryFeePage(@RequestParam(required = false) String studentId,
                               @RequestParam(required = false) String studentName,
                               @RequestParam(required = false) String feeItem,
                               @RequestParam(required = false) String paymentStatus,
                               @RequestParam(required = false) Integer majorId,
                               @RequestParam(required = false) Integer classId,
                               @RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "10") Integer pageSize
                               ) {
        FeeQuery feeQuery = new FeeQuery();
        feeQuery.setStudentId(studentId);
        feeQuery.setStudentName(studentName);
        feeQuery.setFeeItem(feeItem);
        feeQuery.setPaymentStatus(paymentStatus);
        feeQuery.setMajorId(majorId);
        feeQuery.setClassId(classId);
        feeQuery.setPage(page);
        feeQuery.setPageSize(pageSize);
        log.info("分页查询缴费记录，参数：{}", feeQuery);
        PageResult<FeeVo> pageResult = feeService.queryFeePage(feeQuery);
        return Result.success(pageResult);
    }
    @GetMapping("/student/{studentId}")
    @ApiOperation("根据学生ID查询缴费记录")
    public Result queryFeeByStudentId(@PathVariable String studentId) {
        log.info("根据学生ID查询缴费记录，学生ID：{}", studentId);
        List<FeeVo> feeVos = feeService.queryFeeByStudentId(studentId);
        return Result.success(feeVos);
    }
    @PostMapping
    @ApiOperation("新增缴费项目")
    public Result addFee(@RequestBody FeeDTO feeDTO) {
        log.info("新增缴费项目，参数：{}", feeDTO);
        Fee fee = new Fee();
        BeanUtils.copyProperties(feeDTO, fee);
        fee.setPaymentStatus("未支付");
        //设置当前时间
        fee.setPaymentDate(LocalDateTime.now());
        boolean result = feeService.save(fee);
        return result ? Result.success("新增成功") : Result.error("新增失败");
    }
    @PutMapping
    @ApiOperation("修改缴费项目")
    public Result updateFee(@RequestBody FeeDTO feeDTO) {
        log.info("修改缴费项目，参数：{}", feeDTO);
        Fee fee = new Fee();
        BeanUtils.copyProperties(feeDTO, fee);
        boolean result = feeService.updateById(fee);
        return result ? Result.success("修改成功") : Result.error("修改失败");
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除缴费项目")
    public Result deleteFee(@PathVariable Integer id) {
        log.info("删除缴费项目，ID：{}", id);
        boolean result = feeService.removeById(id);
        return result ? Result.success("删除成功") : Result.error("删除失败");
    }

    @PutMapping("/payment/{feeId}")
    @ApiOperation("更新支付状态")
    public Result updatePaymentStatus(@PathVariable Integer feeId,
                                              @RequestParam String paymentStatus,
                                              @RequestParam(required = false) String paymentMethod) {
        log.info("更新支付状态，缴费ID：{}，支付状态：{}，支付方式：{}", feeId, paymentStatus, paymentMethod);
        boolean result = feeService.updatePaymentStatus(feeId, paymentStatus, paymentMethod);
        return result ? Result.success("更新成功") : Result.error("更新失败");
    }
    @PostMapping("/batch")
    @ApiOperation("批量创建缴费项目")
    public Result batchCreateFeeItems(@RequestBody List<FeeDTO> feeDTOs) {
        log.info("批量创建缴费项目，数量：{}", feeDTOs.size());
        List<Fee> fees = feeDTOs.stream().map(dto -> {
            Fee fee = new Fee();
            BeanUtils.copyProperties(dto, fee);
            return fee;
        }).collect(java.util.stream.Collectors.toList());

        boolean result = feeService.batchCreateFeeItems(fees);
        return result ? Result.success("批量创建成功") : Result.error("批量创建失败");
    }

    @PostMapping("/export")
    @ApiOperation("导出缴费记录")
    public Result export(HttpServletResponse response, @RequestParam List<Long> ids) throws IOException {
        log.info("导出缴费记录，Ids：{}", ids);
        return feeService.export(response, ids);
    }

    @GetMapping("/statistics")
    @ApiOperation("获取缴费统计信息")
    public Result getFeeStatistics() {
        log.info("获取缴费统计信息");
        FeeVo statistics = feeService.getFeeStatistics();
        return Result.success(statistics);
    }
}
